package com.zlj.accountant.services;

import java.sql.SQLException;

import com.j256.ormlite.dao.RawRowMapper;
import com.zlj.accountant.core.AbstractService;
import com.zlj.accountant.core.HandlerManager;
import com.zlj.accountant.core.ann.Publish;
import com.zlj.accountant.db.dao.DBHelper;
import com.zlj.accountant.db.entity.TBank;
import com.zlj.accountant.db.entity.TBankPhone;

public class BankPublicService extends AbstractService{

	public BankPublicService(DBHelper db){
	    super(db);
    }
	
	@Publish
	protected final class GetBankByPhoneProvider extends OneKeyResultProvider<TBank, String>{

		@Override
        public TBank provide(HandlerManager handler, String key)
                throws Exception
        {
			String sql = new StringBuilder()
				.append(" Select ")
				.append(" T.").append(TBank.Field_Code).append(", ")
				.append(" T.").append(TBank.Field_Name).append(" ")
				.append(" FROM ").append(TBank.TableName).append(" AS T,")
				.append(TBankPhone.TableName).append(" AS T1 WHERE T.").append(TBank.Field_Code).append(" = T1.").append(TBankPhone.Field_BankCode)
				.append(" AND T1.").append(TBankPhone.Field_PhoneNumber).append(" = ? ").toString();
	       return getDbHelper().getDao(TBank.class).queryRaw(sql,new RawRowMapper<TBank>(){

				@Override
                public TBank mapRow(String[] column, String[] result)
                        throws SQLException
                {
					TBank bank = new TBank();
					bank.setCode(result[0]);
					bank.setName(result[1]);
	                return bank;
                }
	        	
	        }, key).getFirstResult();
        }
		
	}

}
